﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Entities;
using C.Unite;
using ChanExt;
using MySoft.Data;
using Newtonsoft.Json.Linq;

namespace ArtLand.OA.Project.ADM
{
    public partial class Adm_MatterChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string deleteScript = GetDeleteScript();
                JObject defaultObj = new JObject();
                defaultObj.Add("MATNUB", "");
                defaultObj.Add("MatterModel", "");
                defaultObj.Add("MatterSpec", "");
                defaultObj.Add("MatterColor", "");
                defaultObj.Add("Quantity", 0);
                defaultObj.Add("MatterPrice", 0);
                defaultObj.Add("Delete", String.Format("<a href=\"javascript:;\" onclick=\"{0}\"><img src=\"{1}\"/></a>", deleteScript, IconHelper.GetResolvedIconUrl(Icon.Delete)));

                btnNew.OnClientClick = Grid1.GetAddNewRecordReference(defaultObj, false);


                txt_MATNUB.OnClientTriggerClick = Window1.GetSaveStateReference(txt_MATNUB.ClientID)
                       + Window1.GetShowReference("/ADM/Adm_BASICDView.aspx?t=1");
                // DataBaseHelper.BindDrp(Drp_BAUNIT, EnumData.DataBaseEunm.Unit);
                LoadData();
                txt_MatterAdder.Text = CookInfo.EAdminTrueName();
                txt_MatterAdderID.Text = CookInfo.EAdminEmpID();

                //DataBaseHelper.BindDrp(Drp_MatterUnit, EnumData.DataBaseEunm.Unit);
                // txt_MATNUB.OnClientTriggerClick = Window1.GetSaveStateReference(txt_MATNUBID.ClientID, txt_MATNUB.ClientID)
                //         + Window1.GetShowReference("/ADM/Adm_BASICDView.aspx");
                FormInfo.ShowTriggerWin(Window1, txt_MatterAdder, txt_MatterAdderID, "/PublicModel/SelectUsers.aspx?single=1", "选择领料人");
            }
        }
        // 删除选中行的脚本
        private string GetDeleteScript()
        {
            return Confirm.GetShowReference("删除选中行？", String.Empty, MessageBoxIcon.Question, Grid1.GetDeleteSelectedReference(), String.Empty);
        }
        protected void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Adm_Matter inr = TbsDbSession.Default.From<Adm_Matter>().Where(Adm_Matter._.ID == int.Parse(Request["id"])).ToFirst();
                if (inr != null)
                {
                    //txt_Description.Text = inr.Description;
                    //txt_MATNUB.Text = inr.MATNUB;
                    //txt_MATNUBID.Text = StringHelper.GetSubStringList(inr.MATNUB, '-')[0];
                    //txt_MatterAdder.Text = inr.MatterAdder;
                    //txt_AMOUNT.Text = inr.MatterNum.Value.ToString();
                    ////  txt_MatterAdderID.Text = inr.MatterAddid.Value.ToString();
                    //txt_NeedTime.Text = inr.NeedTime.Value.ToString("yyyy-MM-dd");
                    //txt_REMARK.Text = inr.Remark;
                    //Drp_MatterType.SelectedValue = inr.MatterType.Value.ToString();
                    ////  Drp_MatterUnit.SelectedValue = inr.MatterUnit;
                    ////Drp_Status.SelectedValue = inr.Status;
                    //txt_MatterColor.Text = inr.MatterColor;
                    //txt_MatterModel.Text = inr.MatterModel;
                    //txt_MatterSpec.Text = inr.MatterSpec;
                    //MatterUse.Text = inr.MatterUse;
                    //txt_MATNUBID.Text = inr.MATNUBID.ToString();
                    //txt_MOVQYT.Text = inr.MOVQYT.Value.ToString();
                    //txt_REMARK.Text = inr.REMARK;
                    //txt_WHTFOR.Text = inr.WHTFOR;
                    //txt_UPrice.Text = inr.UPrice.Value.ToString();
                }

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            //Adm_BASICD basid = TbsDbSession.Default.From<Adm_BASICD>().Where(Adm_BASICD._.MATNUB == txt_MATNUBID.Text).ToFirst();
            List<Dictionary<string, string>> newAddedList = Grid1.GetNewAddedList();

            if (newAddedList.Count == 0)
            {
                Alert.ShowInTop("请添加物料信息！");
                return;
            }
            else
            {
                Adm_Matter Exp = new Adm_Matter();
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    //    Exp.ID = int.Parse(Request["id"]);
                    //    Exp.Attach();
                    //    Exp.AddTime = DateTime.Now;
                    //    Exp.MATNUB = txt_MATNUB.Text;
                    //    Exp.MATNUBID = int.Parse(txt_MATNUBID.Text);
                    //    Exp.MatterAdder = txt_MatterAdder.Text;
                    //    // Exp.MatterAddid = int.Parse(txt_MatterAdderID.Text);
                    //    Exp.MatterColor = txt_MatterColor.Text;
                    //    Exp.MatterModel = txt_MatterModel.Text;
                    //    Exp.MatterSpec = txt_MatterSpec.Text;
                    //    Exp.MatterType = int.Parse(Drp_MatterType.SelectedValue);
                    //    Exp.MatterUse = MatterUse.Text;
                    //    Exp.NeedTime = Convert.ToDateTime(txt_NeedTime.Text);
                    //    Exp.Remark = txt_REMARK.Text;
                    //    //Exp.Status = //Drp_Status.SelectedValue;
                    //    Exp.MatterUnit = basid.BAUNIT;
                    //    returnvalue = TbsDbSession.Default.Update<Adm_Matter>(Exp);
                }
                else
                {
                    Exp.DeAttach();
                    Exp.AddTime = DateTime.Now;
                    Exp.MatterAdder = txt_MatterAdder.Text;
                    Exp.MatterAddid = int.Parse(txt_MatterAdderID.Text);
                    Exp.MatterType = int.Parse(Drp_MatterType.SelectedValue);
                    if (Drp_MatterType.SelectedValue == "1")
                        Exp.NeedTime = Convert.ToDateTime(txt_NeedTime.Text);
                    Exp.Remark = txt_REMARK.Text;
                    Exp.Status = Drp_Status.SelectedValue;
                    returnvalue = TbsDbSession.Default.Insert<Adm_Matter>(Exp);
                    if (returnvalue > 0)
                    {
                        foreach (var item in newAddedList)
                        {
                            List<string> ls = StringHelper.GetSubStringList(item["MATNUB"], '-');
                            Adm_BASICD basicd = TbsDbSession.Default.From<Adm_BASICD>().Where(Adm_BASICD._.MaterNo == ls[0].Trim()).ToFirst();
                            Adm_MatterChild matterChild = new Adm_MatterChild();
                            matterChild.DeAttach();
                            matterChild.MaterNo = basicd.MaterNo;
                            matterChild.MatterColor = basicd.MaterColor;
                            matterChild.MatterModel = basicd.MaterModel;
                            matterChild.MatterBrand = basicd.MaterBrand;
                            matterChild.Quantity = int.Parse(item["Quantity"]);
                            matterChild.MatterPrice = decimal.Parse(item["MatterPrice"]);
                            matterChild.MatterUnit = basicd.MaterUnit;
                            matterChild.MatnubTitle = basicd.DESTXT;
                            matterChild.MatterID = returnvalue;
                            TbsDbSession.Default.Insert<Adm_MatterChild>(matterChild);
                        }
                    }
                }
                if (returnvalue > 0)
                {
                    Alert.Show("更新成功！", "提示", ActiveWindow.GetHideRefreshReference());
                }
            }
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {

        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void Grid1_AfterEdit(object sender, GridAfterEditEventArgs e)
        {
            //List<Dictionary<string, string>> newAddedList = Grid1.GetNewAddedList();

            //foreach (var item in newAddedList)
            //{
            //    if (item.ContainsKey("MATNUB"))
            //    {
            //        List<string> ls=StringHelper.GetSubStringList(item["MATNUB"],'-');
            //        Adm_BASICD bas = TbsDbSession.Default.From<Adm_BASICD>().Where(Adm_BASICD._.MATNUB == ls[0].Trim()).ToFirst();
            //        Grid1.Rows[e.RowIndex].Values[1] = bas.DESTXT;
            //       // Alert.ShowInTop(item["MATNUB"]);
            //    }
            //}
            //  Dictionary<int, Dictionary<string, string>> modifiedDict = Grid1.GetModifiedDict();
            //  Alert.ShowInTop(modifiedDict.Count.ToString());
        }

        protected void Drp_MatterType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Drp_MatterType.SelectedValue == "2")
            {
                Label16.Hidden = true;
                txt_NeedTime.Hidden = true;
            }
            else
            {
                Label16.Hidden = false;
                txt_NeedTime.Hidden = false;
            }
        }


    }
}